/******************************************************************************
Author: Akshay Dixit
Date Created: April 18, 2018
Date last modified: June 11, 2020 
File Name: 3_in.do
Project: T4D Indonesia 
Purpose: Analysis of secondary outcomes
******************************************************************************/

/* LIST OF SECONDARY OUTCOMES & THE ASSOCIATED VARIABLES
	
	Note that the following codes apply:
		V. Other, specify _________
		X. REFUSED
		Y. DON'T KNOW
		
		95. Other, specify _________
		97. REFUSED
		98. DON'T KNOW
		99. MISSING

RQ.1 Uptake of health services
	
	1. Four or more ANC visits - pf05a_a, pf05a_b
	2. First ANC visit within the first trimester - pf05_a, pf05_b
	3. Birth preparedness - pf45A pf45B pf45C pf45D pf45E pf45F pf45G 
		
RQ.2 Content of health services
	
	1. Antenatal content of care - pf06A pf06B pf06C pf06D pf06E pf07 pf09

RQ.3 Health outcomes

	1. Birth weight - pf12_a pf12_b
	2. Maternal depression - md01 md02 md03 md04 md05 md06


	NOTE: For the ANC binary outcomes in RQ-1, the outcome is 1 ONLY IF if the care
	was provided by a skilled provider.
	
	For the ANC Content of care outcome in RQ-2, however, we consider any kind
	of care, skilled or unskilled.
		
	
*/

clear all
set more off

u "$data/household_data.dta", clear

********************************************************************************

* PREPARING SECONDARY OUTCOMES - RQ.1

***Four or more ANC visits***

g pf05a_diff = pf05a_a - pf05a_b if (pf05a_a != . & pf05a_b != .)
tab pf05a_diff, sort	//The number of ANC visits recorded in card is always lower than that reported by respondent

g number_anc_visits = pf05a_a	//Preferably, use the information from the KIA card 
replace number_anc_visits = pf05a_b if pf05a_a == .	//Where KIA card information is not available, use respondent's reported number
replace number_anc_visits = 0 if pf01 == 3	//Unskilled care regarded as 0 visits
tab number_anc_visits	

g number_anc_visits_binary = (number_anc_visits >= 4)
replace number_anc_visits_binary = . if number_anc_visits == .
tab number_anc_visits_binary

*************************************

***Timing of first ANC visit***

g pf05_diff = pf05_a - pf05_b if (pf05_a != . & pf05_b != .)
tab pf05_diff	//The timing of the first visit reported in the KIA card is generally later than that reported by respondent

g timing_anc_visit = pf05_a //Preferably, use the information from the KIA card 
replace timing_anc_visit = pf05_b if pf05_a == . //Where KIA card information is not available, use respondent's reported number
replace timing_anc_visit = . if pf01 == 3	//Unskilled care regarded as missing
tab timing_anc_visit
				
g anc_first_trimester = (timing_anc_visit <= 13)
replace anc_first_trimester = . if timing_anc_visit == .
tab anc_first_trimester

*************************************

***Birth preparedness***

desc pf45A pf45B pf45C pf45D pf45E pf45F pf45G 

			*Is the skip pattern on pf45F working correctly?
			*pf45F: Identify childcare/who would take care of your other children
			tab sk01 if pf45F == .
			tab sk01a if pf45F == .
			
			*Yes it is. Replace missing values as "Yes"
			replace pf45F = 1 if pf45F == .
			
local bp pf45A pf45B pf45C pf45D pf45E pf45F pf45G 
foreach var of local bp {
	replace `var' = 0 if `var' == 3
	tab `var'
}

egen birth_preparedness = rowtotal(pf45A pf45B pf45C pf45D pf45E pf45F pf45G), missing
sum birth_preparedness
				
********************************************************************************

* PREPARING SECONDARY OUTCOMES - RQ.2

***Antenatal content of care***

desc pf06A pf06B pf06C pf06D pf06E pf07 pf09
count if pf06A == 6 & pf06B == 6 & pf06C == 6 & pf06D == 6 & pf06E == 6 & pf07 == 6 & pf09 == 6

local anc pf06A pf06B pf06C pf06D pf06E pf07 pf09
foreach var of local anc {
	replace `var' = 3 if `var' == 6
	replace `var' = 0 if `var' == 3
	tab `var'
}

egen anc_content = rowtotal(pf06A pf06B pf06C pf06D pf06E pf07 pf09), missing
tab anc_content

	/*
	
	In the various components of ANC content of care, there is a discrepancy
	in the number of times the response "Did not receive ANY antenatal care" is 
	reported. This is probably on account of minor coding errors made by the data collectors. 
	
	In any case, the treatment of "Did not receive any antenatal care" is the same:
	it is treated the same as "No".
	
	*/
	
********************************************************************************

* PREPARING SECONDARY OUTCOMES - RQ.3

***Birth weight***

bysort treatment: sum pf12_a
bysort treatment: sum pf12_b		

g birthweight = pf12_a
replace birthweight = pf12_b if pf12_a == . 

g low_birthweight = (birthweight < 2.5)
replace low_birthweight = . if birthweight == .
tab low_birthweight

***Maternal depression***

desc md01 md02 md03 md04 md05 md06

lab dir
lab drop MD06_EN MD05_EN MD04_EN MD03_EN MD02_EN MD01_EN 

local md md01 md02 md03 md04 md05 md06
foreach var of local md {
	replace `var' = . if `var' == 97 | `var' == 98
	replace `var' = `var' - 1 if `var' != .
	tab `var'
}

egen mother_k6_score = rowtotal(md01 md02 md03 md04 md05 md06), missing
replace mother_k6_score = . if md01 == . | md02 == . | md03 == . | md04 == . | md05 == . | md06 == .
tab mother_k6_score	//The higher the K6 score, the lower the level of depression

	/*
	
	The Harvard Med School doesn't provide any specific guidance on how to treat missing values.
	Here, if any of the 6 components is missing, the total K6 score is coded as missing. 
	This approach is conservative, but even with a conservative approach the number of missing values is low (30).
	
	*/

********************************************************************************

* RESULTS - TABLE

cd "$analysis"


***Label variables***
lab var number_anc_visits_binary "Four or more ANC visits"
lab var anc_first_trimester "First ANC visit within the first trimester"
lab var birth_preparedness "Birth preparedness"
lab var anc_content "Content of Antenatal Care"
lab var low_birthweight "Low birthweight"
lab var mother_k6_score "Maternal depression (K6 score)"
lab var treatment "Treatment"
	
***Regression table***
cap erase "secondary_outcomes.xls"

local outcomes low_birthweight mother_k6_score birth_preparedness number_anc_visits_binary anc_first_trimester anc_content 
foreach var of local outcomes {
	sleep 2000
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	qui reg `var' treatment strata1 strata2 strata3, vce(cluster iddesa)
	outreg2 using "secondary_outcomes.xls", append label nonote nor2 keep(treatment) ///
	addtext(Control Mean, "`rounded'") ///
	addnote("Notes: Robust standard errors clustered at the village-level in parentheses.", "All regressions include strata-specific binary variables.", "*** p<0.01, ** p<0.05, * p<0.1")
}

cap erase "secondary_outcomes.txt"

********************************************************************************

* ROBUSTNESS CHECK - ADDING BASELINE OUTCOME TO REGRESSION SPECIFICATION

cap erase "secondary_outcomes_with_baseline.xls"

merge m:1 iddesa using "$data/baseline_outcomes_by_village.dta"

local secondary anc_first_trimester number_anc_visits_binary low_birthweight birth_preparedness anc_content
foreach var of local secondary {
	di "`var'"
	qui reg `var' treatment strata1 strata2 strata3 m_bl_`var', vce(cluster iddesa)
	outreg2 using "secondary_outcomes_with_baseline.xls", append label keep(treatment m_bl_`var')
}

cap erase "secondary_outcomes_with_baseline.txt"

********************************************************************************

clear



